﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using DAL;

namespace Project_V1
{
    public partial class FrmView : Form
    {
        ToolTip tt = new ToolTip();
        private SettingsService objSettingsService = new SettingsService();
        public static FrmViewChart objFrmViewChart = null;
        public static string RunSettingsid = "0";
        public FrmView()
        {
            InitializeComponent();
            this.Load += FrmView_Load;
            this.timer1.Interval = 200;//每秒钟刷新
        }

        private ReportService objReportService = new ReportService();
        public static String TechnicsNumberTmp = "";

        private int reportListId;//数据报表Id号
        private DateTime updateTime;//数据插入时间,也是铆接完成记录的时间

        private int settingsId;//工艺规范号,由下拉框选择
        private ushort multistageId;//多段铆接编号,由下拉框选择

        public bool bRead = false;

        private void FrmView_Load(object sender, EventArgs e)
        {
            this.btn_btFeed2.Enabled = false;
            this.btn_btFeed11.Enabled = false;
            this.btn_btFeed.Enabled = false;
            this.btn_btStart.Enabled = false;
            this.cmb_Settings.Enabled = true;

            #region 初始化chart
            //初始化chart
            this.chart1.Series.Clear();
            //this.chart1.ChartAreas.Clear();

            Series objSeries0 = chart1.Series.Add("质量监测");
            Series objSeries1 = chart1.Series.Add("压力");
            Series objSeries2 = chart1.Series.Add("扭矩");
            Series objSeries3 = chart1.Series.Add("进给速度");
            Series objSeries4 = chart1.Series.Add("旋转速度");


            Series seriesCopy1 = chart1.Series.Add("扭矩_Copy");//添加Series扭矩_Copy
            Series seriesCopy2 = chart1.Series.Add("进给速度_Copy");
            Series seriesCopy3 = chart1.Series.Add("旋转速度_Copy");

            //ChartArea chartArea = chart1.ChartAreas.Add("ChartArea1");
            ChartArea areaSeries1 = chart1.ChartAreas.Add("ChartArea2");
            ChartArea areaSeries2 = chart1.ChartAreas.Add("ChartArea4");
            ChartArea areaSeries3 = chart1.ChartAreas.Add("ChartArea6");

            ChartArea areaAxis1 = chart1.ChartAreas.Add("ChartArea3");
            ChartArea areaAxis2 = chart1.ChartAreas.Add("ChartArea5");
            ChartArea areaAxis3 = chart1.ChartAreas.Add("ChartArea7");

            chart1.ChartAreas["ChartArea1"].Position = new ElementPosition(17, 11, 79, 85);//X，Y，宽，高
            chart1.ChartAreas["ChartArea1"].InnerPlotPosition = new ElementPosition(6, 0, 94, 94);//不包括刻度线和轴标签

            chart1.Series["质量监测"].ChartType = SeriesChartType.Range;
            chart1.Series["质量监测"].Color = Color.SkyBlue;
            chart1.Series["质量监测"].BorderColor = Color.Blue;
            chart1.Series["压力"].Color = Color.Black;
            chart1.ApplyPaletteColors();

            //chart1.ChartAreas[0].AxisX.Title = "进给位移(mm)";
            //chart1.ChartAreas[0].AxisX.TitleFont = new Font("Arial",12,FontStyle.Regular);
            //chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Black;
            //chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Far;


            chart1.ChartAreas[0].AxisX.Minimum = 0;
            chart1.ChartAreas[0].AxisX.Interval = 0.5;
            chart1.ChartAreas[0].AxisY.Maximum = 5;
            chart1.ChartAreas[0].AxisY.Minimum = 0;
            chart1.ChartAreas[0].AxisY.Interval = 1;
            chart1.ChartAreas[0].AxisY.MinorTickMark.Enabled = true;
            chart1.ChartAreas[0].AxisY.MinorTickMark.Size = 0.4F;
            chart1.ChartAreas[0].AxisY.MinorTickMark.Interval = 0.5;
            chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Silver;//网格线
            chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Silver;
            chart1.ChartAreas[0].AxisX.Enabled = AxisEnabled.True;//曲线无数据时显示XY轴
            chart1.ChartAreas[0].AxisY.Enabled = AxisEnabled.True;

            //定义另外3条曲线的ChartArea的属性
            areaSeries1.BackColor = Color.Transparent;
            areaSeries1.BorderColor = Color.Transparent;
            areaSeries1.Position.FromRectangleF(chart1.ChartAreas["ChartArea1"].Position.ToRectangleF());
            areaSeries1.InnerPlotPosition.FromRectangleF(chart1.ChartAreas["ChartArea1"].InnerPlotPosition.ToRectangleF());
            areaSeries1.AxisX.MajorGrid.Enabled = false;//X轴网格，刻度，轴标签
            areaSeries1.AxisX.MajorTickMark.Enabled = false;
            areaSeries1.AxisX.LabelStyle.Enabled = false;
            areaSeries1.AxisY.MajorGrid.Enabled = false;//Y轴网格，刻度，标签
            areaSeries1.AxisY.MajorTickMark.Enabled = false;
            areaSeries1.AxisY.LabelStyle.Enabled = false;
            areaSeries1.AxisY.Maximum = 20;
            areaSeries1.AxisY.Minimum = -5;
            areaSeries1.AxisY.Interval = 5;
            //areaSeries1.AxisX.Maximum = 33;
            //areaSeries1.AxisX.Minimum = 30;
            areaSeries1.AxisX.Minimum = 0;
            areaSeries1.AxisX.Interval = 0.5;
            //areaSeries1.AxisX.Maximum = -38.1;
            //areaSeries1.AxisX.Minimum = -40.1;
            //areaSeries1.AxisX.Interval = 0.5;
            //areaSeries1.AxisX.IntervalOffset = 0.1;
            objSeries2.ChartArea = areaSeries1.Name;

            areaSeries2.BackColor = Color.Transparent;
            areaSeries2.BorderColor = Color.Transparent;
            areaSeries2.Position.FromRectangleF(chart1.ChartAreas["ChartArea1"].Position.ToRectangleF());
            areaSeries2.InnerPlotPosition.FromRectangleF(chart1.ChartAreas["ChartArea1"].InnerPlotPosition.ToRectangleF());
            areaSeries2.AxisX.MajorGrid.Enabled = false;//X轴网格，刻度，轴标签
            areaSeries2.AxisX.MajorTickMark.Enabled = false;
            areaSeries2.AxisX.LabelStyle.Enabled = false;
            areaSeries2.AxisY.MajorGrid.Enabled = false;//Y轴网格，刻度，标签
            areaSeries2.AxisY.MajorTickMark.Enabled = false;
            areaSeries2.AxisY.LabelStyle.Enabled = false;
            areaSeries2.AxisY.Maximum = 30;
            areaSeries2.AxisY.Minimum = 0;
            areaSeries2.AxisY.Interval = 5;
            //areaSeries2.AxisX.Maximum = 33;
            //areaSeries2.AxisX.Minimum = 30;
            areaSeries2.AxisX.Minimum = 0;
            areaSeries2.AxisX.Interval = 0.5;
            //areaSeries2.AxisX.Maximum = -38.1;
            //areaSeries2.AxisX.Minimum = -40.1;
            //areaSeries2.AxisX.Interval = 0.5;
            //areaSeries2.AxisX.IntervalOffset = 0.1;
            objSeries3.ChartArea = areaSeries2.Name;

            areaSeries3.BackColor = Color.Transparent;
            areaSeries3.BorderColor = Color.Transparent;
            areaSeries3.Position.FromRectangleF(chart1.ChartAreas["ChartArea1"].Position.ToRectangleF());
            areaSeries3.InnerPlotPosition.FromRectangleF(chart1.ChartAreas["ChartArea1"].InnerPlotPosition.ToRectangleF());
            areaSeries3.AxisX.MajorGrid.Enabled = false;//X轴网格，刻度，轴标签
            areaSeries3.AxisX.MajorTickMark.Enabled = false;
            areaSeries3.AxisX.LabelStyle.Enabled = false;
            areaSeries3.AxisY.MajorGrid.Enabled = false;//Y轴网格，刻度，标签
            areaSeries3.AxisY.MajorTickMark.Enabled = false;
            areaSeries3.AxisY.LabelStyle.Enabled = false;
            areaSeries3.AxisY.Maximum = 10000;
            areaSeries3.AxisY.Minimum = 0;
            areaSeries3.AxisY.Interval = 2000;
            //areaSeries3.AxisX.Maximum = 33;
            //areaSeries3.AxisX.Minimum = 30;
            areaSeries3.AxisX.Minimum = 0;
            areaSeries3.AxisX.Interval = 0.5;
            //areaSeries3.AxisX.Maximum = -38.1;
            //areaSeries3.AxisX.Minimum = -40.1;
            //areaSeries3.AxisX.Interval = 0.5;
            //areaSeries3.AxisX.IntervalOffset = 0.1;
            objSeries4.ChartArea = areaSeries3.Name;

            areaAxis1.BackColor = Color.Transparent;
            areaAxis1.BorderColor = Color.Transparent;
            areaAxis1.Position.FromRectangleF(chart1.ChartAreas["ChartArea1"].Position.ToRectangleF());
            areaAxis1.InnerPlotPosition.FromRectangleF(chart1.ChartAreas["ChartArea1"].InnerPlotPosition.ToRectangleF());
            areaAxis1.Position.X -= 5;
            areaAxis1.AxisX.LineWidth = 0;
            areaAxis1.AxisX.MajorGrid.Enabled = false;
            areaAxis1.AxisX.MajorTickMark.Enabled = false;
            areaAxis1.AxisX.LabelStyle.Enabled = false;
            areaAxis1.AxisY.MajorGrid.Enabled = false;
            areaAxis1.AxisY.MajorTickMark.Enabled = true;//多Y轴的刻度线
            areaAxis1.AxisY.Enabled = AxisEnabled.True;
            areaAxis1.AxisY.LineColor = chart1.Series["扭矩"].Color;//Y轴颜色
            areaAxis1.AxisY.MajorTickMark.LineColor = chart1.Series["扭矩"].Color;
            areaAxis1.AxisY.MinorTickMark.LineColor = chart1.Series["扭矩"].Color;
            areaAxis1.AxisY.Maximum = 20;
            areaAxis1.AxisY.Minimum = -5;
            areaAxis1.AxisY.Interval = 5;
            areaAxis1.AxisY.MinorTickMark.Enabled = true;
            areaAxis1.AxisY.MinorTickMark.Size = 0.4F;
            areaAxis1.AxisY.MinorTickMark.Interval = 2.5;

            areaAxis2.BackColor = Color.Transparent;
            areaAxis2.BorderColor = Color.Transparent;
            areaAxis2.Position.FromRectangleF(chart1.ChartAreas["ChartArea1"].Position.ToRectangleF());
            areaAxis2.InnerPlotPosition.FromRectangleF(chart1.ChartAreas["ChartArea1"].InnerPlotPosition.ToRectangleF());
            areaAxis2.Position.X -= 10;
            areaAxis2.AxisX.LineWidth = 0;
            areaAxis2.AxisX.MajorGrid.Enabled = false;
            areaAxis2.AxisX.MajorTickMark.Enabled = false;
            areaAxis2.AxisX.LabelStyle.Enabled = false;
            areaAxis2.AxisY.MajorGrid.Enabled = false;
            areaAxis2.AxisY.MajorTickMark.Enabled = true;//多Y轴的刻度线
            areaAxis2.AxisY.Enabled = AxisEnabled.True;
            areaAxis2.AxisY.LineColor = chart1.Series["进给速度"].Color;//Y轴颜色
            areaAxis2.AxisY.MajorTickMark.LineColor = chart1.Series["进给速度"].Color;
            areaAxis2.AxisY.MinorTickMark.LineColor = chart1.Series["进给速度"].Color;
            areaAxis2.AxisY.Maximum = 30;
            areaAxis2.AxisY.Minimum = 0;
            areaAxis2.AxisY.Interval = 5;
            areaAxis2.AxisY.MinorTickMark.Enabled = true;
            areaAxis2.AxisY.MinorTickMark.Size = 0.4F;
            areaAxis2.AxisY.MinorTickMark.Interval = 1;

            areaAxis3.BackColor = Color.Transparent;
            areaAxis3.BorderColor = Color.Transparent;
            areaAxis3.Position.FromRectangleF(chart1.ChartAreas["ChartArea1"].Position.ToRectangleF());
            areaAxis3.InnerPlotPosition.FromRectangleF(chart1.ChartAreas["ChartArea1"].InnerPlotPosition.ToRectangleF());
            areaAxis3.Position.X -= 15;
            areaAxis3.AxisX.LineWidth = 0;
            areaAxis3.AxisX.MajorGrid.Enabled = false;
            areaAxis3.AxisX.MajorTickMark.Enabled = false;
            areaAxis3.AxisX.LabelStyle.Enabled = false;
            areaAxis3.AxisY.MajorGrid.Enabled = false;
            areaAxis3.AxisY.MajorTickMark.Enabled = true;//多Y轴的刻度线
            areaAxis3.AxisY.Enabled = AxisEnabled.True;
            areaAxis3.AxisY.LineColor = chart1.Series["旋转速度"].Color;//Y轴颜色
            areaAxis3.AxisY.MajorTickMark.LineColor = chart1.Series["旋转速度"].Color;
            areaAxis3.AxisY.MinorTickMark.LineColor = chart1.Series["旋转速度"].Color;
            areaAxis3.AxisY.Maximum = 10000;
            areaAxis3.AxisY.Minimum = 0;
            areaAxis3.AxisY.Interval = 2000;
            areaAxis3.AxisY.MinorTickMark.Enabled = true;
            areaAxis3.AxisY.MinorTickMark.Size = 0.4F;
            areaAxis3.AxisY.MinorTickMark.Interval = 500;
            areaAxis3.AxisY.LabelStyle.Angle = 0;

            // Hide copied series
            seriesCopy1.IsVisibleInLegend = false;//不在图例中显示该曲线图例
            seriesCopy1.Color = Color.Transparent;
            seriesCopy1.BorderColor = Color.Transparent;
            seriesCopy1.ChartArea = areaAxis1.Name;
            seriesCopy2.IsVisibleInLegend = false;//不在图例中显示该曲线图例
            seriesCopy2.Color = Color.Transparent;
            seriesCopy2.BorderColor = Color.Transparent;
            seriesCopy2.ChartArea = areaAxis2.Name;
            seriesCopy3.IsVisibleInLegend = false;//不在图例中显示该曲线图例
            seriesCopy3.Color = Color.Transparent;
            seriesCopy3.BorderColor = Color.Transparent;
            seriesCopy3.ChartArea = areaAxis3.Name;

            foreach (Series item in this.chart1.Series)
            {
                item.ChartType = SeriesChartType.Line;//样条图和折线图
                item.BorderWidth = 1;//曲线粗细程度
                item.Points.AddXY(0, 0);
            }
            chart1.Series["质量监测"].ChartType = SeriesChartType.Range;
            //chart1.Series["质量监测"].IsVisibleInLegend = false;//不在图例中显示该曲线图例
            //chart1.Series["质量监测"].Color = Color.Transparent;
            //chart1.Series["质量监测"].BorderColor = Color.Transparent;
            #endregion

            this.cmb_Settings.DropDownStyle = ComboBoxStyle.DropDownList;//下拉列表不可编辑
            this.cmb_Settings.DisplayMember = "SettingsName";
            this.cmb_Settings.ValueMember = "SettingsId";
            this.cmb_Settings.DataSource = CommonMethods.SettingsList;
            this.cmb_Settings.SelectedIndex = -1;

            //this.textBoxControl1.Enabled = false;
            //this.textBoxControl2.Enabled = false;
            //this.textBoxControl3.Enabled = false;
            //this.textBoxControl4.Enabled = false;
            //this.textBoxControl5.Enabled = false;
            //this.textBoxControl6.Enabled = false;
            //this.textBoxControl7.Enabled = false;
            //this.textBoxControl8.Enabled = false;
            //this.textBoxControl9.Enabled = false;
            //this.textBoxControl10.Enabled = false;
            //this.textBoxControl11.Enabled = false;
            //this.textBoxControl12.Enabled = false;
            //this.textBoxControl13.Enabled = false;
            //this.textBoxControl14.Enabled = false;
            //this.textBoxControl15.Enabled = false;

            this.timer1.Enabled = true;//Chart控件绑定好曲线后再开启计时器
            RivetingNOK_tmp = CommonMethods.CurrentValue["_03_HMI.RivetingNOK"];
            RivetingOK_tmp = CommonMethods.CurrentValue["_03_HMI.RivetingOK"];
            RefVacuoValve_Manual_tmp = CommonMethods.CurrentValue["_03_HMI.RefVacuoValve_Manual"];

            DataSync.WritePowerOffValueToPLC(System.Environment.CurrentDirectory + "\\ConfigFile\\Cache.csv");
        }

        //双击事件,将值传给参数修改界面,弹出界面
        private void ModifyTxt_UserControlClick(object sender, EventArgs e)
        {

    }

        public static void UpdateParameter(string VariableName , string val)
        {

            try
            {
                Beckhoff_PLC.IsWriting = true;
                //Thread.Sleep(5);
                if (CommonMethods.CurrentType[VariableName] == "float")
                {
                    if (Beckhoff_PLC.WriteVariable(VariableName, float.Parse(val)))//将每个变量的变量名传来,在Tag中
                    {
                  
                    }
                    else
                    {
                       
                    }
                }

                if (CommonMethods.CurrentType[VariableName] == "bool")
                { 
                    if (val == "1") 
                        val = "True";
                    else if (val == "0")
                        val = "False";

             
                    if (Beckhoff_PLC.WriteVariable(VariableName, bool.Parse(val)))//将每个变量的变量名传来,在Tag中
                    {

                    }
                    else
                    {

                    }
                }



                if (CommonMethods.CurrentType[VariableName] == "ushort")
                {


                    if (Beckhoff_PLC.WriteVariable(VariableName, ushort.Parse(val)))//将每个变量的变量名传来,在Tag中
                    {
                      
                    }
                    else
                    {
                   
                    }
                }

                if (CommonMethods.CurrentType[VariableName] == "int")
                {


                    if (Beckhoff_PLC.WriteVariable(VariableName, Int16.Parse(val)))//将每个变量的变量名传来,在Tag中
                    {
                
                    }
                    else
                    {
                    
                    }
                }

                if (CommonMethods.CurrentType[VariableName] == "UInt32")
                {


                    if (Beckhoff_PLC.WriteVariable(VariableName, UInt32.Parse(val)))//将每个变量的变量名传来,在Tag中
                    {
                      
                    }
                    else
                    {
             
                    }
                }

                Beckhoff_PLC.IsWriting = false;
            }
            catch(Exception ex)
            {
                Beckhoff_PLC.IsWriting = false;
            }
        }

        public static string RivetingOK_tmp = "False", RivetingNOK_tmp = "False", RefVacuoValve_Manual_tmp = "False";

        private void timer1_Tick(object sender, EventArgs e)
        {
            #region TextBox窗体显示随下拉框变化
            if (this.cmb_Settings.SelectedIndex != -1)
            {
                this.btn_btFeed2.Enabled = true;
                this.btn_btFeed11.Enabled = true;
                this.btn_btFeed.Enabled = true;
                this.btn_btStart.Enabled = true;
            }

            #endregion


            //if (CommonMethods.CurrentValue["_03_HMI.RivetingOK"] != RivetingOK_tmp)
            //{
            //    RivetingOK_tmp = CommonMethods.CurrentValue["_03_HMI.RivetingOK"];
            //    if (RivetingOK_tmp == "True"  )
            //    {
            //        bRead = true;

            //    }
            //}
            //textBox1.Text = CommonMethods.CurrentValue["_03_HMI.RivetingNOK"];

            //if (CommonMethods.CurrentValue["_03_HMI.RivetingNOK"] != RivetingNOK_tmp)
            //{
            //    RivetingNOK_tmp = CommonMethods.CurrentValue["_03_HMI.RivetingNOK"];
            //    if (RivetingNOK_tmp == "True"  && CommonMethods.CurrentValue["_03_HMI.RivetingOK"] == "False" )
            //    {
            //        bRead = true; 
            //    }
            //} 

            //CommonMethods.CurrentValue["_03_HMI.RivetingOK"] == "True")//|| CommonMethods.CurrentValue["_03_HMI.RivetingNOK"] == "True") //


   





            if (CommonMethods.CurrentValue["_03_HMI.Feeding"] == "True")
            {
                bRead = true;
            }


            #region TextBox PLC自动下发工艺
            //_03_HMI.SysExcMode && CommonMethods.CurrentValue["_03_HMI.SysExcMode"] == "True"&& CommonMethods.CurrentValue["_03_HMI.SysExcMode"] == "True"


            //
            if (CommonMethods.CurrentValue["_03_HMI.TechnicsNumber"].ToString() != TechnicsNumberTmp )
            {  
                if (CommonMethods.CurrentValue["_03_HMI.TechnicsNumber"].ToString() != "0") {
                    CommonMethods.SendPlcDs = objSettingsService.GetPLC(CommonMethods.CurrentValue["_03_HMI.TechnicsNumber"].ToString());
               
                    if (CommonMethods.SendPlcDs.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < CommonMethods.SendPlcDs.Tables[0].Columns.Count; i++)
                        { 
                            if (CommonMethods.SendPlcDs.Tables[0].Columns[i].ColumnName != "PrePressureSetValue")
                            { 
                                UpdateParameter("_03_HMI." + CommonMethods.SendPlcDs.Tables[0].Columns[i].ColumnName, CommonMethods.SendPlcDs.Tables[0].Rows[0][i].ToString());
                            }
                        }
                         
                    }
                    FrmView.RunSettingsid = CommonMethods.CurrentValue["_03_HMI.TechnicsNumber"].ToString();//20240808
                }
                //FrmView.RunSettingsid = CommonMethods.CurrentValue["_03_HMI.TechnicsNumber"].ToString();
                TechnicsNumberTmp = CommonMethods.CurrentValue["_03_HMI.TechnicsNumber"].ToString();
            }
            #endregion


            ////之前执行多次读取,需改为执行一次读取
            //if (CommonMethods.CurrentValue != null && (CommonMethods.CurrentValue["_03_HMI.RivetingOK"] == "True" || CommonMethods.CurrentValue["_03_HMI.RivetingNOK"] == "True") && bRead )//(CommonMethods.CurrentValue["_03_HMI.RivetingOK"] == "True" || CommonMethods.CurrentValue["_03_HMI.RivetingNOK"] == "True") )//&& bRead
            //{
            //    Beckhoff_PLC.IsReadingArr = true;
            //    Beckhoff_PLC.ReadArray();
            //    bRead = false;
            //    Beckhoff_PLC.IsReadingArr = false;
            //}



            if (CommonMethods.CurrentValue["_03_HMI.RivetingOK"] != RivetingOK_tmp)
            {
                if (CommonMethods.CurrentValue["_03_HMI.RivetingOK"] == "True")
                {
                    Beckhoff_PLC.IsReadingArr = true;
                    Beckhoff_PLC.ReadArray();
                    bRead = false;
                    Beckhoff_PLC.IsReadingArr = false;
                }
                RivetingOK_tmp = CommonMethods.CurrentValue["_03_HMI.RivetingOK"];
            }


            if (CommonMethods.CurrentValue["_03_HMI.RivetingNOK"] != RivetingNOK_tmp)
            {
                if (CommonMethods.CurrentValue["_03_HMI.RivetingNOK"] == "True")
                {
                    Beckhoff_PLC.IsReadingArr = true;
                    Beckhoff_PLC.ReadArray();
                    bRead = false;
                    Beckhoff_PLC.IsReadingArr = false;
                }
                RivetingNOK_tmp = CommonMethods.CurrentValue["_03_HMI.RivetingNOK"];
            }



            if (Beckhoff_PLC.CommOK)
            {


                foreach (Control ct in this.panel3.Controls)
                { 
                    if (ct is TextBoxControl && ct.Tag != null && ct.Tag.ToString().IndexOf(".") != -1)
                    {
                        if (CommonMethods.CurrentValue != null && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            //this.ActualValue = CommonMethods.CurrentValue[this.Tag.ToString()];   //((CheckBox)item)
                            ((TextBoxControl)ct).ActualValue = CommonMethods.CurrentValue[ct.Tag.ToString()];
                        }
                    } 
                    if (ct is Label && ct.Tag != null && ct.Tag.ToString().IndexOf(".") != -1)
                    {
                        if (CommonMethods.CurrentValue != null && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            ct.Text = CommonMethods.CurrentValue[ct.Tag.ToString()];
                        }
                    }
                }


                foreach (Control ct in this.panel1.Controls)
                {
                    if (ct is Label && ct.Tag != null && ct.Tag.ToString().IndexOf(".") != -1)
                    {
                        if (CommonMethods.CurrentValue != null && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            ct.Text = CommonMethods.CurrentValue[ct.Tag.ToString()];
                        }
                    }
                }


                foreach (Control ct in this.panel2.Controls)//panel2内控件,判断并赋值变更颜色
                { 
                    if (ct.Name.ToString().ToLower().IndexOf("circle") != -1 && ct.Tag.ToString().IndexOf(".") != -1)
                    {
                        if (ct.Tag.ToString() == "_02_InOutput.biVacuoValueRch1")
                        { 
                            if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                            {
                                ct.BackColor = Color.LimeGreen;
                            }
                            else
                            {
                                ct.BackColor = Color.Olive;
                            }
                        }  
                    }


                    if (ct is Label && ct.Tag != null && ct.Tag.ToString().IndexOf(".") != -1
                        && ct.Tag.ToString() != "_03_HMI.SysIsReady"
                        && ct.Tag.ToString() != "_03_HMI.FeedingOK"
                        && ct.Tag.ToString() != "_03_HMI.PrePressingOK"
                        && ct.Tag.ToString() != "_03_HMI.RivetingOK"
                        && ct.Tag.ToString() != "_02_InOutput.biVacuoValueRch1"
                        )
                    {  
                        if (CommonMethods.CurrentValue != null && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            ct.Text = CommonMethods.CurrentValue[ct.Tag.ToString()];
                        }
                    }
                    else if (ct is Button && ct.Tag != null && ct.Tag.ToString() != "" && ct.Tag.ToString().IndexOf(".") != -1)
                    {

                        if(CommonMethods.CurrentValue["_03_HMI.RefVacuoValve_Manual"] != RefVacuoValve_Manual_tmp)
                        {
                            if(CommonMethods.CurrentValue["_03_HMI.RefVacuoValve_Manual"] == "False")
                            { 
                                Beckhoff_PLC.WriteVariable("_03_HMI.btManualInhaleRivet", false);
                            }
                            RefVacuoValve_Manual_tmp = CommonMethods.CurrentValue["_03_HMI.RefVacuoValve_Manual"]; 
                        }


                        if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            ct.BackColor = Color.FromArgb(60, 179, 113);
                        }
                        else
                        {
                            ct.BackColor = Color.FromArgb(255, 255, 255);
                        }
                    }
                }

                #region 刷新按钮和标签的文字和颜色                
                if (CommonMethods.CurrentValue["_03_HMI.btEnableVacuoValve"] == "True")//吸钉按钮
                {
                    btn_PressConfirmed.BackColor = Color.FromArgb(60, 179, 113);
                }
                else
                {
                    btn_PressConfirmed.BackColor = Color.FromArgb(255, 255, 255);
                }

                if (CommonMethods.CurrentValue["_03_HMI.SysIsReady"] == "True")
                {
                    this.lbl_SysIsReady.BackColor = Color.Green;
                    this.lbl_SysIsReady.Text = "系统就绪";
                }
                else
                {
                    this.lbl_SysIsReady.BackColor = Color.Red;
                    this.lbl_SysIsReady.Text = "系统故障";
                }




                if (CommonMethods.CurrentValue["_03_HMI.FeedingOK"] == "True")
                {
                    this.lbl_Feeding.BackColor = Color.Green;
                    this.lbl_Feeding.Text = "送钉成功";
                }
                else if (CommonMethods.CurrentValue["_03_HMI.FeedingNOK"] == "True")
                {
                    this.lbl_Feeding.BackColor = Color.Yellow;
                    this.lbl_Feeding.Text = "送钉不成功";
                } 
                else if (CommonMethods.CurrentValue["_03_HMI.Feeding"] == "True")
                {
                    this.lbl_Feeding.BackColor = Color.LightBlue;
                    this.lbl_Feeding.Text = "送钉进行中";
                }
                else
                {
                    this.lbl_Feeding.BackColor = Color.Gray;
                    this.lbl_Feeding.Text = "送钉状态";
                }




                if (CommonMethods.CurrentValue["_03_HMI.PrePressingOK"] == "True")
                {
                    this.lbl_PrePressing.BackColor = Color.Green;
                    this.lbl_PrePressing.Text = "预压成功";
                }
                else if (CommonMethods.CurrentValue["_03_HMI.PrePressingNOK_OverTime"] == "True")
                {
                    this.lbl_PrePressing.BackColor = Color.Yellow;  
                        this.lbl_PrePressing.Text = "预压超时"; 
                }
                else if (CommonMethods.CurrentValue["_03_HMI.PrePressingNOK_NOTVacuo"] == "True")
                {
                    this.lbl_PrePressing.BackColor = Color.Yellow;  
                        this.lbl_PrePressing.Text = "预压掉真空"; 
                }
                else if (CommonMethods.CurrentValue["_03_HMI.PrePressingNOK_OverPosition"] == "True")
                {
                    this.lbl_PrePressing.BackColor = Color.Yellow; 
                        this.lbl_PrePressing.Text = "预压超限";
                } 
                else if (CommonMethods.CurrentValue["_03_HMI.PrePressing"] == "True")
                {
                    this.lbl_PrePressing.BackColor = Color.LightBlue;
                    this.lbl_PrePressing.Text = "预压进行中";
                }
                else
                {
                    this.lbl_PrePressing.BackColor = Color.Gray;
                    this.lbl_PrePressing.Text = "预压状态";

                }

                 

                if (CommonMethods.CurrentValue["_03_HMI.RivetingOK"] == "True")
                {
                    this.lbl_Riveting.BackColor = Color.Green;
                    this.lbl_Riveting.Text = "铆接成功";
                } 
                    else if (CommonMethods.CurrentValue["_03_HMI.Rivet1ingNOK_OverTime"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "1段铆接超时";
                    }
                    else if (CommonMethods.CurrentValue["_03_HMI.Rivet1ingNOK_OverSpeed"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "1段铆接超速";
                    }
                else if(CommonMethods.CurrentValue["_03_HMI.Rivet1ingNOK_OverPosition"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "1段铆接超限";
                    }
                else if (CommonMethods.CurrentValue["_03_HMI.Rivet2ingNOK_OverTime"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "2段铆接超时";
                    }
                    else if (CommonMethods.CurrentValue["_03_HMI.Rivet2ingNOK_OverSpeed"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "2段铆接超速";
                    }
                else if(CommonMethods.CurrentValue["_03_HMI.Rivet2ingNOK_OverPosition"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "2段铆接超限";
                    }
                else if(CommonMethods.CurrentValue["_03_HMI.Rivet3ingNOK_OverTime"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "3段铆接超时";
                    }
                    else if (CommonMethods.CurrentValue["_03_HMI.Rivet3ingNOK_OverSpeed"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "3段铆接超速";
                    }
                else if (CommonMethods.CurrentValue["_03_HMI.Rivet3ingNOK_OverPosition"] == "True")
                    {
                        this.lbl_Riveting.BackColor = Color.Yellow;
                        this.lbl_Riveting.Text = "3段铆接超限";
                    } 
                else if (CommonMethods.CurrentValue["_03_HMI.Riveting"] == "True")
                {
                    this.lbl_Riveting.BackColor = Color.LightBlue;
                    this.lbl_Riveting.Text = "铆接进行中";
                }
                else
                {
                    this.lbl_Riveting.BackColor = Color.Gray;
                    this.lbl_Riveting.Text = "铆接状态";

                }
                #endregion









            }

            #region 通讯数据存入集合
            if (Beckhoff_PLC.IsReadingArrOK)//通讯数据已经存入集合,执行数据插入和绑定曲线数据
            {
                if (RunSettingsid != "0")
                {
                                updateTime = DateTime.Now;  //FrmView.RunSettingsid
                                settingsId = Convert.ToInt32(RunSettingsid);// this.cmb_Settings.SelectedValue);
                                try
                                {
                                    reportListId = objReportService.AddReportList(settingsId, updateTime);
                                }
                                catch (Exception ex)
                                {
                                     MessageBox.Show(ex.Message);
                                }
               

                            try
                            {
                                if (objReportService.Import(Beckhoff_PLC.ProcessDataList, reportListId))//将集合信息存入数据库
                                {
                                    //MessageBox.Show("数据保存成功", "提示信息");
                                }
                                else
                                {
                                     //MessageBox.Show("数据保存失败", "提示信息");
                                }
                            }
                            catch (Exception ex)
                            {
                                 MessageBox.Show(ex.Message);
                            }

                            foreach (Series item in this.chart1.Series)
                            {
                                item.Points.Clear();
                            }
                            //chart1.Series["质量监测"].ChartType = SeriesChartType.Range;
                            //chart1.Series["质量监测"].Color = Color.SkyBlue;
                            //chart1.Series["质量监测"].BorderColor = Color.Blue;
                            chart1.Series["质量监测"].Points.DataBindXY(Beckhoff_PLC.X, Beckhoff_PLC.UpperError, Beckhoff_PLC.LowerError);
                            chart1.Series["压力"].Points.DataBindXY(Beckhoff_PLC.X, Beckhoff_PLC.Y1);
                            chart1.Series["扭矩"].Points.DataBindXY(Beckhoff_PLC.X, Beckhoff_PLC.Y2);
                            chart1.Series["进给速度"].Points.DataBindXY(Beckhoff_PLC.X, Beckhoff_PLC.Y3);
                            chart1.Series["旋转速度"].Points.DataBindXY(Beckhoff_PLC.X, Beckhoff_PLC.Y4);

                            foreach (DataPoint point in chart1.Series["扭矩"].Points)
                            {
                                chart1.Series["扭矩_Copy"].Points.AddXY(point.XValue, point.YValues[0]);
                            }
                            foreach (DataPoint point in chart1.Series["进给速度"].Points)
                            {
                                chart1.Series["进给速度_Copy"].Points.AddXY(point.XValue, point.YValues[0]);
                            }
                            foreach (DataPoint point in chart1.Series["旋转速度"].Points)
                            {
                                chart1.Series["旋转速度_Copy"].Points.AddXY(point.XValue, point.YValues[0]);
                            }
                }
                Beckhoff_PLC.IsReadingArrOK = false;//保证只执行一次
                this.cmb_Settings.Enabled = true;
            }
            #endregion

        }

        #region 按钮通讯写入操作

        private void btn_btFeed_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable(".btManualReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_btFeed_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable(".btManualReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_btFeed2_MouseDown(object sender, MouseEventArgs e)
        {
            this.cmb_Settings.Enabled = false;
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable(".btPrePress", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_btFeed2_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable(".btPrePress", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_btStart_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable(".btStart", true);
                Beckhoff_PLC.IsWriting = false;
                bRead = true;//可以读取结构体数组缓存
            }
            btrig = false;
        }
        private void btn_btStart_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable(".btStart", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        #endregion

        private void cmb_Settings_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmb_Settings.SelectedIndex >= 0)
            {
                settingsId = Convert.ToInt32(RunSettingsid);//this.cmb_Settings.SelectedValue
                for (int i = 0; i < CommonMethods.SettingsList.Count; i++)
                {
                    if (CommonMethods.SettingsList[i].SettingsId == settingsId)
                    {
                        multistageId = Convert.ToUInt16(CommonMethods.SettingsList[i].MultistageId);
                    }
                }
                bool btrig = true;
                if (btrig)
                {
                    Beckhoff_PLC.IsWriting = true;
                    Beckhoff_PLC.WriteVariable(".MultistageId", multistageId);
                    Beckhoff_PLC.IsWriting = false;
                }
                btrig = false;
            }
        }

        //单击打开曲线界面
        private void chart1_Click(object sender, EventArgs e)
        {
            objFrmViewChart = null;
            if (objFrmViewChart == null)
            {
                objFrmViewChart = new FrmViewChart();
                objFrmViewChart.FormBorderStyle = FormBorderStyle.FixedSingle;
                objFrmViewChart.ShowDialog();
            }
            else
            {
                objFrmViewChart.Activate();//激活只能在最小化的时候起作用
                objFrmViewChart.WindowState = FormWindowState.Normal;
            }
        }

        private void btn_btFeed11_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable(".btFeed", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_btFeed11_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable(".btFeed", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_PressConfirmed_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btEnableVacuoValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable(".btEnableVacuoValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btEnableVacuoValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable(".btEnableVacuoValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void FrmView_Load_1(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void lbl_SysIsReady_Click(object sender, EventArgs e)
        {

        }

        private void lbl_Feeding_Click(object sender, EventArgs e)
        {

        }

        private void label58_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            bool teest = false;
            string val = "1";
            if (val == "1")
                val = "true";
            else
                val = "false";


            teest = bool.Parse(val);
           // if (Beckhoff_PLC.WriteVariable(VariableName, bool.Parse(val))



            CommonMethods.SendPlcDs = objSettingsService.GetPLC(textBox1.Text); 
            if (CommonMethods.SendPlcDs.Tables.Count > 0)
            {
                for (int i = 0; i < CommonMethods.SendPlcDs.Tables[0].Columns.Count; i++)
                {
                    //Beckhoff_PLC.IsWriting = true;
                    Thread.Sleep(5);
                    UpdateParameter("_03_HMI." + CommonMethods.SendPlcDs.Tables[0].Columns[i].ColumnName, CommonMethods.SendPlcDs.Tables[0].Rows[0][i].ToString());
                   // Beckhoff_PLC.IsWriting = false;
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                var area = chart1.ChartAreas[0];
                var area1 = chart1.ChartAreas[1];
                var area2 = chart1.ChartAreas[2];
                var area3 = chart1.ChartAreas[3];
                var area8 = chart1.ChartAreas[8];
                double yValue = area.AxisY.PixelPositionToValue(e.Y);
                double yValue1 = area1.AxisY.PixelPositionToValue(e.Y);
                double yValue2 = area2.AxisY.PixelPositionToValue(e.Y);
                double yValue3 = area3.AxisY.PixelPositionToValue(e.Y);
                double yValue8 = area8.AxisY.PixelPositionToValue(e.Y);

                tt.SetToolTip(chart1, "压力:" + yValue.ToString("F2") + "\r" +
                                  "扭矩:" + yValue1.ToString("F2") + "\r" +
                                  "进给速度:" + yValue2.ToString("F2") + "\r" +
                                  "旋转速度:" + yValue3.ToString("F2") + "\r" +
                                  "进给位移:" + yValue8.ToString("F2") + " "
                                  );


            }
            catch
            {

            }

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label13_Click(object sender, EventArgs e)
        {
            //Beckhoff_PLC.IsWriting = true;
            //if (CommonMethods.CurrentValue["_02_InOutput.biVacuoValueRch1"] == "False")
            //{ 
            //    Beckhoff_PLC.WriteVariable("_02_InOutput.biVacuoValueRch1", true);
            //}
            //if (CommonMethods.CurrentValue["_02_InOutput.biVacuoValueRch1"] == "True")
            //{
            //    Beckhoff_PLC.WriteVariable("_02_InOutput.biVacuoValueRch1", false);
            //}
            //Beckhoff_PLC.IsWriting = false;
        }

        private void btn_btManualInhaleRivet_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btManualInhaleRivet"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btManualInhaleRivet", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btManualInhaleRivet"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btManualInhaleRivet", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_FbtManualFeed_Click(object sender, EventArgs e)
        {

        }

        private void btn_FbtManualFeed_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btManualFeed", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;

        }

        private void btn_FbtManualFeed_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btManualFeed", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_btManualPrePress_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btManualPrePress", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;

        }

        private void btn_btManualPrePress_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btManualPrePress", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_btManualRivet_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btManualRivet", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void btn_btManualRivet_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btManualRivet", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }



  
    }

 
}